package com.dormitoryAdministratorSystem.dao.student.impl;

import com.dormitoryAdministratorSystem.dao.student.IStudentDao;
import com.dormitoryAdministratorSystem.entity.student.StudentBean;
import com.dormitoryAdministratorSystem.util.BeansUtil;
import com.dormitoryAdministratorSystem.util.DBUtil;
import com.dormitoryAdministratorSystem.util.DBUtil2;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements IStudentDao {
    @Override
    public List<StudentBean> getStudents(String sql, Object... parms) {
        return getList(sql,parms);
    }

    @Override
    public int getstudentTolNum(String sql, Object... parms) {
        int result = 0;

        ResultSet rSet = DBUtil.excuteQuery(sql, parms);
        try {
            while(rSet.next()){
                result = rSet.getInt("sNum");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }

        return result;
    }

    @Override
    public int insertStudentInRoom(StudentBean studentBean) {
        int result = 0;

        String sql = "insert into t_student values(?, ?, ?, ?, ?, ?, ?, ?)";
        result = DBUtil2.executeUpdate(
                sql,
                studentBean.getSId(),
                studentBean.getClassBean().getCId(),
                studentBean.getRoomBean().getRId(),
                studentBean.getSName(),
                studentBean.getGender(),
                studentBean.getPhoneNum(),
                studentBean.getPhotoPath(),
                studentBean.getSTime()
        );

        return result;
    }

    @Override
    public int updateStudentInRoom(StudentBean studentBean) {
        int result = 0;

        String sql = "update t_student set " +
                "cid = ?, " +
                "rid = ?, " +
                "sname = ?, " +
                "gender = ?, " +
                "sPhoneNo = ?, " +
                "photoPath = ?, " +
                "sTime = ? " +
                "where sid = ?";
        result = DBUtil2.executeUpdate(
                sql,
                studentBean.getClassBean().getCId(),
                studentBean.getRoomBean().getRId(),
                studentBean.getSName(),
                studentBean.getGender(),
                studentBean.getPhoneNum(),
                studentBean.getPhotoPath(),
                studentBean.getSTime(),
                studentBean.getSId()
        );

        return result;
    }

    @Override
    public StudentBean getStudentById(int studentId) {
        StudentBean studentBean = null;

        String sql = "select " +
                "sid, cid, rid, sname, gender, sPhoneNo, photoPath, sTime " +
                "from t_student where sid = ? ";
        ResultSet rSet = DBUtil.excuteQuery(sql,studentId);

        try{
            while(rSet.next()){
                studentBean = getStudent(rSet);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }

        return studentBean;
    }

    @Override
    public List<StudentBean> getStudentByClassId(int classId) {

        String sql = "select " +
                "sid, cid, rid, sname, gender, sPhoneNo, photoPath, sTime " +
                "from t_student where cid = ? ";
        return getList(sql,classId);
    }

    @Override
    public List<StudentBean> getStudentByRoomId(int roomId) {
        String sql = "select " +
                "sid, cid, rid, sname, gender, sPhoneNo, photoPath, sTime " +
                "from t_student where rid = ? ";
        return getList(sql,roomId);
    }

    private StudentBean getStudent(ResultSet rSet) throws SQLException {
        StudentBean s = new StudentBean();
        s.setSId(rSet.getInt("sid"));
        s.setSName(rSet.getString("sname"));
        s.setGender(rSet.getString("gender"));
        s.setPhoneNum(rSet.getString("sPhoneNo"));
        s.setPhotoPath(rSet.getString("photoPath"));
        s.setSTime(rSet.getDate("sTime"));
        s.setClassBean(BeansUtil.getClass(rSet.getInt("cid")));
        s.setRoomBean(BeansUtil.getRoom(rSet.getInt("rid")));

        return  s;
    }

    private List<StudentBean> getList(String sql, Object... parms){
        List<StudentBean> list = new ArrayList<>();
        ResultSet rSet = DBUtil.excuteQuery(sql,parms);
        try{
            while(rSet.next()){
                list.add(getStudent(rSet));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }

        return list;
    }
}
